package com.example.config;

import com.example.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfg implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    /**
     * 生产环境
     * @param registry
     */
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        //设置允许跨域的路径
//        registry.addMapping("/**")
//                //设置允许跨域请求的域名
//                //当**Credentials为true时，**Origin不能为星号，需为具体的ip地址【如果接口不带cookie,ip无需设成具体ip】
//                /* 放自己的前端域名*/
//                .allowedOrigins("http://localhost:5173", "http://127.0.0.1:5173", "http://127.0.0.1:8082", "http://192.168.47.63:5173","https://16e068ee.r2.cpolar.top/","192.168.221.92:5173")
////                .allowedOrigins("*")
//                //是否允许证书 不再默认开启
//                .allowCredentials(true)
//                //设置允许的方法
//                .allowedMethods("PUT", "POST", "GET", "DELETE", "OPTIONS")
//                //设置允许的头部信息
//                .allowedHeaders("Content-Type", "Content-Length", "Authorization", "Accept", "X-Requested-With", "X-App-Id", "X-Token")
//                //跨域允许时间
//                .maxAge(3600);
//    }



    /**
     * 测试环境
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:5173", "http://127.0.0.1:5173")
//                .allowedOriginPatterns("*")
                // 允许携带 Cookie
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("Content-Type", "Authorization", "X-Requested-With")
                .maxAge(3600);
    }



    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(loginInterceptor)
//                .addPathPatterns("/**") // 拦截所有请求
//                .excludePathPatterns(
//                        "/**/add*/",
//                        "/**/login",
//                        "/doc.html/**",  // 排除Knife4j的路径
//                        "/webjars/**",
//                        "/swagger-resources/**",
//                        "/v2/api-docs",
//                        "/**/OPTIONS",
//                        "/swagger-ui.html/**"  // 排除Swagger UI的路径（如果有）
//                );
        registry.addInterceptor(loginInterceptor).excludePathPatterns("/**");
//        registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**").excludePathPatterns(
//                "/**/login",
//                "/**/add*/",
//                "/doc.html/**",
//                "/webjars/**",
//                "/swagger-resources/**",
//                "/v2/api-docs",
//                "/**/OPTIONS",
//                "/swagger-ui.html/**"
//        );

    }
}